package com.pzy.base.util;

import org.springframework.http.MediaType;

import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;

public class HttpServletRequestUtil {
    /**
     * 判断是否文件上传请求
     * 
     * @param request
     * @return true: 是文件上传请求, false: 非文件上传请求
     */
    public static boolean isMultipartRequest(ServletRequest request) {
        if (request instanceof HttpServletRequest) {
            return HttpServletRequestUtil.isMultipartRequest((HttpServletRequest) request);
        } else {
            return false;
        }
    }

    /**
     * 判断是否文件上传请求
     * 
     * @param request
     * @return true: 是文件上传请求, false: 非文件上传请求
     */
    public static boolean isMultipartRequest(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.toLowerCase().startsWith("multipart/");
    }

    /**
     * 判断请求的mediatype是否为 application/json
     * 
     * @param request
     * @return
     */
    public static boolean isJsonRequest(ServletRequest request) {
        if (request instanceof HttpServletRequest) {
            return HttpServletRequestUtil.isJsonRequest((HttpServletRequest) request);
        } else {
            return false;
        }
    }

    /**
     * 判断请求的mediatype是否为 application/json
     * 
     * @param request
     * @return
     */
    public static boolean isJsonRequest(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.toLowerCase().contains(MediaType.APPLICATION_JSON_VALUE);
    }
}
